package com.scrapy.http.pojo;

import com.scrapy.http.body.HttpBody;
import com.scrapy.http.get.HttpClient;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 封装每个主页面的信息
 * Created by wanter on 2016/11/8.
 */
public class HtmlPage {
    /**
     * 本页面的url地址
     */
    private String indexUrl;
    /**
     * 下一个页面的url地址
     */
    private String nextUrl;
    /**
     * 是否还有下一页
     */
    private boolean next;

    public List<String> getJokeUrl() {
        return jokeUrl;
    }

    /**
     * 本页面所有的笑话的链接
     */
    private List<String> jokeUrl;
    /**
     * 对传入的url进行解析
     * @param startUrl
     * @param properties
     */
    public HtmlPage(String startUrl, Properties properties) {
        if(startUrl.endsWith("/")){
            startUrl = startUrl.substring(0,startUrl.length()-1);
        }
        this.indexUrl = startUrl;
        this.next = false;
        jokeUrl = new ArrayList<>();
        paseThisHtml(startUrl,properties);
    }

    /**
     * 解析出html页面中需要的信息
     * @param startUrl 主要页面所包含的内容
     * @param properties 正则表达式的存放位置
     */
    private void paseThisHtml(String startUrl, Properties properties){
        HttpClient httpClient = new HttpClient(startUrl);
        HttpBody httpBody = httpClient.getHttpBody();
        String htmlBodey = httpBody.getCountxt();
        String nextUrlRegex = properties.getProperty("nextPageUrl");
        this.nextUrl = getNexPageUrl(htmlBodey,nextUrlRegex);
        Pattern pattern = Pattern.compile(properties.getProperty("htmlPage"));
        Matcher matcher = pattern.matcher(htmlBodey);
        while (matcher.find()){
            String jokeDiv = matcher.group();
            String jokeRegex = properties.getProperty("jokeUrl");
            String url = getIndexJokeUrl(jokeDiv,jokeRegex);
            //设置笑话的链接
            jokeUrl.add(url);
        }


    }

    /**
     * 解析出本条笑话url
     * @param content 当前所以页面的body
     * @param regex 笑话本身的url
     * @return 本页所有的笑话的链接
     */
    private String getIndexJokeUrl(String content, String regex){
        String strs[] = content.split("href=\"/");

        String temp = "";
        for (String str : strs){
            if(str.contains("contentHerf")){
                temp=this.addPrefix("/"+str.substring(0,str.indexOf("\"")));
                //System.out.println(temp);
                return temp;
            }

        }
        return "失败数据";

    }

    /**
     * 解析出下一条页面的url
     * @param divBody
     * @param regex
     * @return
     */
    private String getNexPageUrl(String divBody, String regex){

        /**
         * 问老师 这个地方
         */

        //System.out.println(regex);
        //regex = "((<li><a href=\")(.*?)(\" rel=\"nofollow\">)(<!--<a href=\")(.*?)(\" rel=\"nofollow\">--><span class=\"page-numbers\">))";
        Pattern pattern = Pattern.compile(regex);
        //System.out.println(regex);
        Matcher matcher = pattern.matcher(divBody);
        String trueUrl = "失败数据";
        while(matcher.find()){
            trueUrl = this.addPrefix(matcher.group(3));
        }
        //System.out.println(trueUrl);
        return trueUrl;
    }

    /**
     * 给爬到的半截url封装成完整的url
     * @param prefix  后半截url地址
     * @return 返回完整的url地址
     */
    private String addPrefix(String prefix){
        //String str[] = prefix.split("/");
        //System.out.println(str[2]);
        if(this.indexUrl.endsWith("com")){
            return this.indexUrl +prefix;
        }else {
            String url[] = this.indexUrl.split("com");
            return url[0]+"com"+prefix;
        }
    }

    public String getIndexUrl() {

        return indexUrl;
    }

    public String getNextUrl() {
        return nextUrl;
    }

    /**
     * 检测本条链接是不是35条链接
     * @return 返回还有下一条链接
     */
    public boolean hashNext() {
        String str[] = this.getNextUrl().split("page");
        if(str[1].contains("35")){
            if(!this.getIndexUrl().split("page")[1].contains("35"))
                next = false;
        }else {
            next = true;
        }
        return next;
    }
}
